<template>
  <div class="container-content">
    <el-form
      ref="formRef"
      :model="kingBomEntity"
      label-width="90px"
      label-position="left"
    >
     
      <div class="desc-title">安全库存清单</div>
      <el-table
        :data="kingBomEntity.warehouseMaxMinInventoryEntity"
        style="width: 100%"
        highlight-current-row
        border
        :key="flag"
      >
        <template #empty
          ><el-button plain type="primary" round @click="addNewRow"
            >添加新行</el-button
          ></template
        >
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              plain
              icon="el-icon-plus"
              size="small"
              @click="handleDataAdd(scope.$index, scope.row)"
            ></el-button>
            <el-button
              plain
              icon="el-icon-minus"
              size="small"
              @click="handleDataDelete(scope.$index, scope.row)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" width="50" align="center">
        </el-table-column>
        <el-table-column label="仓库"  align="center">
          <template slot-scope="scope">
            <el-form-item label-width="0px" style="margin: 0">
              <el-input
                v-model="scope.row.warehouseName"
              >
                <i
                slot="suffix"
                class="el-icon-search"
                @click="openList('仓库', scope.$index)"
              ></i>
              </el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="物料编码"  align="center">
          <template slot-scope="scope">
         <el-form-item label-width="0px" style="margin: 0">
              <el-input
                v-model="scope.row.materialCode"
              >
                <i
                slot="suffix"
                class="el-icon-search"
                @click="openList('物料编码', scope.$index)"
              ></i>
              </el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="物料名称" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.materialName" disabled></el-input>
          </template>
        </el-table-column>
        <el-table-column label="基本单位"  align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.baseUnit" disabled></el-input>
          </template>
        </el-table-column>
        <el-table-column label="最小库存"  align="center">
          <template slot-scope="scope">
             <el-input v-model="scope.row.minInventory"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          label="安全库存"
        
          align="center"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.safeInventory"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="在订货点" align="center">
          <template slot-scope="scope">
             <el-input v-model="scope.row.reorderPoint"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="经济订货批量"  align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.economicOrderNum"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="最大库存"  align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.maxInventory"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="备注"  align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.remark"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        width="60%"
        :title="title"
        :visible.sync="dialogTableVisible"
        :append-to-body="list"
      >
        <el-table
          :data="gridData"
           @row-dblclick="getRow"
          v-if="title == '仓库'"
        >
       <el-table-column property="wareCode" label="编码"></el-table-column>
        <el-table-column property="wareName" label="名称"></el-table-column>
        <el-table-column property="address" label="仓库地址"></el-table-column>
        <el-table-column
          property="wareManagement"
          label="仓库负责人"
        ></el-table-column>
        <el-table-column property="address" label="仓库属性"></el-table-column>
        <el-table-column
          property="address"
          label="第三方仓储类型"
        ></el-table-column>
        <el-table-column property="" label="数据状态"></el-table-column>
        <el-table-column property="" label="允许即使库负库存"></el-table-column>
        <el-table-column property="" label="启用仓位管理"></el-table-column>
        <el-table-column property="" label="不参与可发量统计"></el-table-column>
        <el-table-column property="" label="参与预警"></el-table-column>
        </el-table>
        <el-table
          :data="gridData"
           @row-dblclick="getRow"
          v-if="title == '物料编码'"
        >
         <el-table-column property="materialCode" label="编码"></el-table-column>
        <el-table-column property="materialName" label="名称"></el-table-column>
        <el-table-column property="model" label="规格型号"></el-table-column>
        <!-- <el-table-column property="isChange" label="数据状态"></el-table-column>
        <el-table-column property="isUsed" label="禁用状态"></el-table-column> -->
        <!-- <el-table-column
          property="materialProperty"
          label="物料属性"
        ></el-table-column> -->
        <el-table-column property="purchaseUnitName" label="基本单位"></el-table-column>
        </el-table>
         <span slot="footer" class="dialog-footer">
            <el-button @click="dialogTableVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogTableVisible = false"
              >确 定</el-button
            >
          </span>
      </el-dialog>
    </el-form>
  </div>
</template>
<script>
import {
  editMaxMinInventory,
  getMaterial,
  getKingDocTypeList,
 MaxMinInventory,
  getWarehouseList
} from "@/api/bim/bim";
export default {
  data() {
    return {
      options: [
        {
          label: "采购",
          value: "C",
        },
        {
          label: "自制",
          value: "Z",
        },
        {
          label: "委外",
          value: "W",
        },
      ],
      options1: [
        {
          label: "标准件",
          value: "1",
        },
        {
          label: "返还件",
          value: "2",
        },
        {
          label: "替代件",
          value: "3",
        },
      ],
      options2: [
        {
          label: "固定",
          value: "1",
        },
        {
          label: "变动",
          value: "2",
        },
        {
          label: "阶梯",
          value: "3",
        },
      ],
      dialogTableVisible: false,
      title: "",
      list: true,
      num: 0,
      count: 0,
      flag: 0,
      kingBomEntity: {
        warehouseMaxMinInventoryEntity: [],
        
      },
      dataList: [],
      gridData: [],
    };
  },
  methods: {
    // 获取每次点击的内容进行区分，并在此方法内进行打开弹窗及数据的请求
    openList(item, i) {
      this.count = i;
      this.dialogTableVisible = true;
      this.title = item;
      if (this.title == "仓库") {
        getWarehouseList().then((res) => {
          if (res.code == 200) {
            this.gridData = res.rows;
            
          }
        });
      }else if (this.title=='物料编码') {
          getMaterial().then((res) => {
          if (res.code == 200) {
         
            this.gridData = res.data.records;
          }
        });
      } 
    },
    // 获取点击的表格内容并且关闭弹窗且进行内容的回显
    getRow(row) {
      this.dialogTableVisible = false;
      if (this.title == "仓库") {
           this.kingBomEntity.warehouseMaxMinInventoryEntity[this.count].warehouseCode =
          row.wareCode;
           this.kingBomEntity.warehouseMaxMinInventoryEntity[this.count].warehouseName =
          row.wareName;
        this.flag = 1;
      } else if (this.title == "物料编码") {
        this.kingBomEntity.warehouseMaxMinInventoryEntity[this.count].materialCode = row.materialCode;
        this.kingBomEntity.warehouseMaxMinInventoryEntity[this.count].materialName = row.materialName;
        this.kingBomEntity.warehouseMaxMinInventoryEntity[this.count].purchaseUnitName = row.baseUnit;
          this.flag = 2;
      }
    },

    getKingDocType() {
      getKingDocTypeList({}).then((res) => {
        // console.log(res);
      });
    },

    selectProject() {
      getMaterial({
        pageNo: 1,
        pageSize: 5,
        materialProperty: 2,
        allowProduce: 1,
      }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.records;
        }
      });
    },
    handleDataAdd() {
      if (this.kingBomEntity.warehouseMaxMinInventoryEntity == undefined) {
        this.kingBomEntity.warehouseMaxMinInventoryEntity = [];
      }
      let outher = {
        bomId: "",
        childBomVersion: "",
        childMaterialCode: "",
        childMaterialName: "",
        childType: "",
        childUnitCode: "",
        createBy: "",
        createTime: "",
        fixedLoss: "",
        id: "",
        pageNo: 0,
        pageSize: 0,
        params: {},
        remark: "",
        remarks: "",
        searchValue: "",
        sendDeptCode: "",
        sendWarehouseCode: "",
        supplyDeptCode: "",
        supplyType: "",
        updateBy: "",
        updateTime: "",
        usageDenominator: "",
        usageNumerator: "",
        usageType: "",
        varLossRate: "",
      };
      this.kingBomEntity.warehouseMaxMinInventoryEntity.push(outher);
    },
    handleDataDelete(index, row) {
      this.$confirm("请确认是否要删除此行？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.kingBomEntity.warehouseMaxMinInventoryEntity.splice(index, 1);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addNewRow() {
      this.handleDataAdd();
    },
    onSubmit() {
      this.kingBomEntity.warehouseMaxMinInventoryEntity.id = new Date();
      // console.log(this.kingBomEntity);
      editMaxMinInventory(this.kingBomEntity).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: "新增成功！",
            type: "success",
          });
        }
      });
    },
  },
  created() {
    this.selectProject();
    // console.log(this.listid);
   MaxMinInventory(this.listid).then((res)=>{
    if (res.code==200) {
        this.kingBomEntity=res.data
    }
   })
  },
};
</script>
<style lang="scss" scoped>
.divider {
  margin: 0;
}
.el-icon-search {
  margin-top: 70% !important;
}
</style>
